package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description
 * @Author bo.li
 * @Date 2023/3/16 9:40
 * @Version 1.0
 */
@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper,WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private FileStorageService fileStorageService;

    /** 收藏标志 */
    public final static int COLLECTION = 1;

    @Override
    public ResponseResult upload(MultipartFile multipartFile) {

        //aaa.jpg  ->  xxx.jpg
        String orgFileName = multipartFile.getOriginalFilename();
        String postfix = orgFileName.substring(orgFileName.lastIndexOf("."));
        //新文件名，防止冲突被覆盖  21132-asdfa-dafa.jpg
        String newFileName = UUID.randomUUID().toString() + postfix;
        String url = "";
        //文件上传到minio
        try {
            url = fileStorageService.uploadImgFile("", newFileName, multipartFile.getInputStream());
        }catch (Exception e){
            log.error("上传文件异常",e);
            return ResponseResult.errorResult(AppHttpCodeEnum.UPLOAD_FAIL);
        }

        //保存素材表
        WmMaterial material = new WmMaterial();
        material.setUrl(url);
        material.setType((short)0);
        material.setUserId(WmThreadLocalUtil.getUser().getId());
        material.setIsCollection((short)0);
        material.setCreatedTime(new Date());
        wmMaterialMapper.insert(material);

        return ResponseResult.okResult(material);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //检查参数
        dto.checkParam();

        IPage page = new Page(dto.getPage(),dto.getSize());

        //构造查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection() == COLLECTION,WmMaterial::getIsCollection,dto.getIsCollection());
        wrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        wrapper.orderByDesc(WmMaterial::getCreatedTime);

        this.page(page,wrapper);

        //构造返回对象
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 收藏素材
     * @param id
     */
    @Override
    public void updateCollect(Integer id) {
        updateById(WmMaterial.builder().id(id).isCollection(WmMaterial.IS_COLLECTION).build());
    }

    /**
     * 取消收藏素材
     * @param id
     */
    @Override
    public void updateCancelCollect(Integer id) {
        updateById(WmMaterial.builder().id(id).isCollection(WmMaterial.NOT_COLLECTION).build());

    }


}